'''
Created on Apr 6, 2011

@author: Tyler
'''
import os, fnmatch, cv, string

if __name__ == '__main__':
    pass


def load_images(dirName, extension):
    lst = []
    for fileName in os.listdir(dirName):
        if fnmatch.fnmatch(fileName, extension):
            lst.append(cv.LoadImage(dirName + fileName, cv.CV_LOAD_IMAGE_GRAYSCALE))
    return lst

"""groupIds = {'armsUpX': 0,
            'bendOver': 1, 
            'boogieNights': 2,
            'cross': 3,
            'lickStamp': 4,
            'lowerTwoArms': 5,
            'open': 6,
            'reply': 7,
            'run': 8,
            'stretch': 9,
            'thrust': 10,
            'twoArms': 11,
            'thinker': 12,
            'superman': 13,
            'basket': 14}"""
            
groupIds = {'armsUpX': 0,
            'bendOver': 1, 
            'boogieNights': 2,
            'cross': 3,
            'lickStamp': 4,
            #'lowerTwoArms': 5,
            'open': 6,
            'reply': 7,
            #'stretch': 9,
            #'thrust': 10,
            #'twoArms': 11,
            #'thinker': 12,
            #'superman': 13,
            'basket': 14,
            'confusion': 15,
            'bending': 16}

if True:
    dirName = '../temp_data/'
    f=open('../train.tab', 'w')
else:
    dirName = '../training_set/'
    f=open('../training.tab', 'w')
extension = '.png'
vecLength = 7
data = "Class\t" + "\t".join(["f"+repr(i) for i in range(vecLength)]) + "\n"
data = data + "d"+ ("\tc"*vecLength) + "\n"
data = data + "class" + "\n"
for k,v in groupIds.items():
    imgs = load_images(dirName, k + '*')
    for im in imgs:
        moments = cv.Moments(cv.GetMat(im))
        featureVec = []
        featureVec.append(cv.GetNormalizedCentralMoment(moments,1,1))
        featureVec.append(cv.GetNormalizedCentralMoment(moments,0,2))
        featureVec.append(cv.GetNormalizedCentralMoment(moments,2,0))
        featureVec.append(cv.GetNormalizedCentralMoment(moments,1,2))
        featureVec.append(cv.GetNormalizedCentralMoment(moments,2,1))
        featureVec.append(cv.GetNormalizedCentralMoment(moments,0,3))
        featureVec.append(cv.GetNormalizedCentralMoment(moments,3,0))
        #featureVec.extend(cv.GetHuMoments(moments)) #returns an additional 7
        featureVecStr = repr(featureVec)
        featureVecStr = string.replace(featureVecStr,'[','')
        featureVecStr = string.replace(featureVecStr,']','')
        featureVecStr = string.replace(featureVecStr,', ',"\t")
        featureVecStr = k + "\t" + featureVecStr
        print featureVecStr
        data = data + featureVecStr + "\n"
        
f.write(data+"\n")
f.close()